package com.jutou.wap.Controller;

import java.io.File;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.druid.pool.DruidPooledConnection;
import com.alibaba.fastjson.JSONObject;
import com.jutou.wap.dao.TestDaoInterface;
import com.jutou.wap.jdbc.JdbcConnection;

@Controller
public class TestController {
	@Autowired(required=true)
	private TestDaoInterface testDaoInterface;
	@Autowired(required=true)
	private JdbcConnection jdbcConnection;
	
	/*@Autowired
	private HttpServletRequest request;*/
	@RequestMapping(value="/index")
	public ModelAndView index(HttpServletRequest request ,HttpServletResponse response){
		String name = request.getParameter("name");
		request.setAttribute("name", name);
		System.out.println(testDaoInterface.getTest());
		try {
			DruidPooledConnection call =  (DruidPooledConnection) jdbcConnection.getConnection();			
			ResultSet pare =  call.getConnection().prepareStatement("select * from t_user limit 0,10").executeQuery();
			int i =0;
			while(pare.next()){
				System.out.println(pare.getString(1));
			}
			System.out.println(i+"===");
			//System.out.println(call.execute("select * from  t_user limit 0,10"));
			//System.out.println(call.getString("user_id"));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return new ModelAndView("index");
	}
	
	@ResponseBody
	@RequestMapping(value="/getfile",produces="text/plain;charset=UTF-8")
	public String getFile(HttpServletRequest request) {
		CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
				request.getSession().getServletContext());
		String dir =  request.getParameter("dir");
		String json ="";
		JSONObject j =  new JSONObject();
		if (multipartResolver.isMultipart(request)) {
			MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
			Iterator<String> iter = multipartRequest.getFileNames();
			while (iter.hasNext()) {
				String jsonName = (String) iter.next();
				MultipartFile file = multipartRequest.getFile(jsonName);
				String fileName = file.getOriginalFilename();
				if(!fileName.contains(".")){
					j.put(jsonName, "图片不能为空");
					return j.toJSONString();
				}
				fileName = fileName.substring(fileName.indexOf("."), fileName.length());
				String path = dir + System.currentTimeMillis()+ fileName;
				json = path;
				j.put(jsonName, json);			
				File localFile = new File(path);
				try {
					file.transferTo(localFile);
				} catch (IllegalStateException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return j.toJSONString();
	}
	
	
}
